package com.itcast.demoone

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class PageOneActivity : AppCompatActivity() {

    // 定义一个常量
    private val TAG = "itHeiMa-PageOne"

    override fun onCreate(savedInstanceState: Bundle?) {

        Log.d(TAG, "-------onCreate")

        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_page_one)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        // ------- 正常的跳转方式 -----------------
        // 1. 获取按钮
        /*val btn = findViewById<View>(R.id.btn_action_next)
        btn.setOnClickListener {
           // 1.1 创建一个Intent对象
           val intent = Intent(this, PageTwoActivity::class.java)

            // 1.2 添加数据
            intent.putExtra("user_name", "张三")
            intent.putExtra("user_age", 18)
            intent.putExtra("user_sex", "男")


           // 1.3 启动Activity
           startActivity(intent)
        }*/

        // ------- 跳转方式 2(返回可以带结果回来) -----------------
        val resLauncher =  registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result ->
            if (result.resultCode == RESULT_OK) {
               val data = result.data?.getStringExtra("back_data")
               Toast.makeText(this, "返回的数据: $data", Toast.LENGTH_SHORT).show()
            }
        }

        val btn = findViewById<View>(R.id.btn_action_next)
        btn.setOnClickListener {
            // 1.1 创建一个Intent对象
            val intent = Intent(this, PageTwoActivity::class.java)

            // 1.2 添加数据
            intent.putExtra("user_name", "张三")
            intent.putExtra("user_age", 18)
            intent.putExtra("user_sex", "男")


            // 1.3 启动Activity
            resLauncher.launch(intent)
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "-------onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "-------onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "-------onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "-------onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "-------onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "-------onRestart")
    }
}